package servlet.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.bean.UserBean;
import servlet.model.ServiceFactory;
import servlet.publicUtils.MyUtils;

public class UserInfoModifyServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//request.setCharacterEncoding("UTF-8");
		String strOperationType = request.getParameter("opt");
		int iOpt = MyUtils.ObjtoInt(strOperationType);
		String strJumpPage = "";
		//判断是否进入哪个界面
		if(iOpt==0){
			// 准备用户进行修改的数据
			// 获取id主键值
			String strUid = request.getParameter("uid");
			UserBean userB = ServiceFactory.getUserInfoManagerServiceInstance().getUserInfoById(strUid);
			// 将数据缓存到session对象中
			request.setAttribute("userB", userB);
			strJumpPage = "logined/add.jsp";
			
		}else if(iOpt==1){
			//删除
		int userId = MyUtils.ObjtoInt(request.getParameter("uid"));
			// 数据库删除

		int i = ServiceFactory.getUserInfoManagerServiceInstance().deleteUserById(userId);
			// 跳转页面

		strJumpPage = "UserInfoModifyServlet?opt=2";

		}else if(iOpt==2){
			//查询
			UserBean userB = (UserBean) MyUtils.getNewInstance(UserBean.class);
			
			userB.setUsername(request.getParameter("username"));
			userB.setUser_age(MyUtils.ObjtoInt(request.getParameter("age")));
			userB.setUser_age2(MyUtils.ObjtoInt(request.getParameter("age2")));
			userB.setUser_sex(request.getParameter("sex"));
			userB.setUser_address(request.getParameter("address"));
			userB.setUser_phone(request.getParameter("phone"));
			
			//查询操作
			List<UserBean> lstUser=ServiceFactory.getUserInfoManagerServiceInstance().queryUserInfo(userB);
			request.getSession().setAttribute("lstUser", lstUser);
			strJumpPage = "logined/userInfoAddDetail.jsp";
			
			
			
		}
		
		// 跳转到修改页面
		request.getRequestDispatcher(strJumpPage).forward(request, response);
	}

}
